package tp.pr2;

public class Interpreter {
	private static final String LINE_SEPARATOR = System
			.getProperty("line.separator");

	/**
	 * Genera una nueva instruccion de acuerdo a lo introducido por el usuario
	 * 
	 * @param line
	 *            - un string con el input del usuario
	 * @return la nueva instruccion
	 * 
	 */
	public Instruction generateInstruction(String line) {
		Instruction instruction = null;
		String[] words;
		words = line.split(" ");

		if (words.length < 1 || words.length > 2)
			instruction = new Instruction();

		else if (words.length == 1) {
			if (words[0].equalsIgnoreCase("HELP"))
				instruction = new Instruction(Action.HELP);
			else if (words[0].equalsIgnoreCase("MOVE"))
				instruction = new Instruction(Action.MOVE);
			else if (words[0].equalsIgnoreCase("QUIT"))
				instruction = new Instruction(Action.QUIT);
			else
				instruction = new Instruction();
		} else if (words.length == 2) {
			if (words[0].equalsIgnoreCase("TURN")) {
				if (words[1].equalsIgnoreCase("LEFT"))
					instruction = new Instruction(Action.TURN, Rotation.LEFT);
				else if (words[1].equalsIgnoreCase("RIGHT"))
					instruction = new Instruction(Action.TURN, Rotation.RIGHT);
				else
					instruction = new Instruction(Action.TURN);
			} else
				instruction = new Instruction();
		}
		return instruction;
	}

	/**
	 * Devuelve informacion sobre las instrucciones entendidas por el robot
	 * 
	 * @return un string con todas las instrucciones disponibles
	 */
	public String interpreterHelp() {
		String help = Message.VALID_INSTRUCTIONS;
		Action[] actions = Action.values();
		for (int i = 0; i < actions.length - 1; i++) { // No mostramos la ultima
			help += LINE_SEPARATOR; // Action, UNKNOWN.
			help += "  " + actions[i].toString();
			if (actions[i] == Action.TURN)
				help += " <LEFT | RIGHT>";
		}

		return help;
	}
}
